package com.icesoft.system.controller;

import com.icesoft.framework.core.controller.BaseCrudRestController;
import com.icesoft.framework.core.vo.JsonResult;
import com.icesoft.framework.core.vo.Pagination;
import com.icesoft.system.entity.Article;
import com.icesoft.system.dto.ArticleDTO;
import com.icesoft.system.permission.OperationCons;
import com.icesoft.system.permission.annotation.BindPermission;
import com.icesoft.system.vo.ArticleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 文章Controller
 *
 */
@Slf4j
@RestController
@BindPermission(name = "文章")
@RequestMapping("/system/article")
public class ArticleController extends BaseCrudRestController<Article> {

	/**
	 * 查询ViewObject的分页数据
	 * <p>
	 * url请求参数示例: /list?field=abc&pageSize=20&pageIndex=1&orderBy=id
	 * </p>
	 *
	 */
	@GetMapping("/query")
	@BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
	public JsonResult<List<ArticleVO>> getViewObjectListMapping(ArticleDTO entity, Pagination pagination) throws Exception {
		return super.getViewObjectList(entity, pagination, ArticleVO.class);
	}

	/**
	 * 根据资源id查询ViewObject
	 */
	@GetMapping("/{id}")
	@BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
	public JsonResult<ArticleVO> getViewObjectMapping(@PathVariable("id") Long id) throws Exception {
		return super.getViewObject(id, ArticleVO.class);
	}

	/**
	 * 新建
	 */
	@PostMapping("/create")
	@BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
	public JsonResult<Map<String, Object>> createEntityMapping(@Valid @RequestBody ArticleDTO dto) throws Exception {
		return super.createEntity(dto);
	}

	/**
	 * 更新
	 */
	@PostMapping("/update/{id}")
	@BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
	public JsonResult<?> updateEntityMapping(@PathVariable("id") Long id, @Valid @RequestBody ArticleDTO dto) throws Exception {
		return super.updateEntity(id, dto);
	}

	/**
	 * 根据id删除资源对象
	 */
	@GetMapping("/delete/{id}")
	@BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
	public JsonResult<?> deleteEntityMapping(@PathVariable("id") Long id) throws Exception {
		return super.deleteEntity(id);
	}
}
